import {
  getRegion,
  addAddress,
  updateAddress,
  loginWithCode
} from "../../../../api.js"
const TIP_MAP = {
  "name": "请填写收货人姓名",
  "phone": "请填写手机号",
  "province": "请选择所在地区",
  "city": "请选择所在地区",
  "district": "请选择所在地区",
  "address": "请填写详细地址"
}
Page({
  data: {
    address: {}
  },
  onLoad: function (options) {
    if (options.address) {
      let address = JSON.parse(options.address)
      this.setData({
        isUpdate: true,
        address
      })
    }
  },
  // input 绑定 data-key 作为键值 
  inputModel(e) {
    let key = e.target.dataset.key
    let value = e.detail.value
    key = `address.${key}`
    this.setData({
      [key]: value
    })
  },
  // picker 选中事件
  handleAddressPick(e) {
    let valArr = e.detail.value
    this.setData({
      "address.province": valArr[0],
      "address.city": valArr[1],
      "address.district": valArr[2]
    })
  },
  // 新增
  handleAddressAdd() {
    let validateKey = Object.keys(TIP_MAP)
    let token = getApp().globalData.Store.token
    for (let i = 0, l = validateKey.length; i < l; i++) {
      let current = this.data.address[validateKey[i]]
      if (!current || current === "") {
        return wx.showToast({
          title: TIP_MAP[validateKey[i]],
          icon: "none"
        })
      }
    }
    // 限制点击次数
    if (this.data.onreq) return;
    this.setData({
      onreq: true
    })
    addAddress({
      ...this.data.address,
      token
    }).then(res => {

      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code === 666) {
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      } else {
        this.setData({
          onreq: false
        })
      }

    })
  },
  // 默认地址
  defaultCheck() {
    let _default = this.data.address.is_default === 1 ? 0 : 1
    this.setData({
      "address.is_default": _default
    })
  },
  // 修改地址
  handleAddressUpdate(e) {
    let validateKey = Object.keys(TIP_MAP)
    let token = getApp().globalData.Store.token
    for (let i = 0, l = validateKey.length; i < l; i++) {
      let current = this.data.address[validateKey[i]]
      if (!current || current === "") {
        return wx.showToast({
          title: TIP_MAP[validateKey[i]],
          icon: "none"
        })
      }
    }
    // 限制点击次数
    if (this.data.onreq) return;
    this.setData({
      onreq: true
    })
    updateAddress({
      ...this.data.address,
      token
    }).then(res => {
      if (res.data.code === 666) {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      } else if (res.data.code == 111111) {
        this.setData({
          onreq: false
        })
        this.checkUserInfo()
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
        this.setData({
          onreq: false
        })
      }
    })
  },
  upadd: function () {
    var that = this
    if (wx.chooseAddress) {
      wx.chooseAddress({
        success: function (res) {
          that.data.address.name = res.userName
          that.data.address.phone = res.telNumber
          that.data.address.province = res.provinceName
          that.data.address.city = res.cityName
          that.data.address.district = res.countyName
          that.data.address.address = res.detailInfo
          that.setData({
            address: that.data.address
          })
        },
        fail: function (err) {
        }
      })
    } else {
    }
  },
  // 检查用户信息授权
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          xp.setData({
            isAuth: false
          })
          wx.navigateTo({
            url: '/pages/shouquan/shouquan',
          })
        } else {
          xp.setData({
            isAuth: true
          })
          wx.getUserInfo({
            success(res) {
              xp.loginWithCode(res.userInfo)
            }
          })
        }
      }
    })
  },
  //登陆
  loginWithCode(userInfo) {
    const xp = this
    wx.login({
      success({
        code
      }) {
        loginWithCode({
          ...userInfo,
          code,
          business_id: getApp().globalData.businessId
        }).then(res => {
          if (res.data.code === 666) {
            getApp().globalData.Store = res.data.data
            xp.handleAddressUpdate()
          }
        })
      }
    })
  },
  onShow() {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  }
})